package MT::TypeKey;
use strict;
use warnings;

use base qw( MT::ErrorHandler );

use vars qw( $VERSION );
$VERSION = 0.4;

use Authen::TypeKey 0.05;
use File::Spec;

# Add sign functionality if fallback for Authen::TypeKey::Sign is plausable.
# sub sign { }

#--- object methods

sub new {
    my $class = shift;
    my $app   = shift;
    $class .= "::" . $app;
    eval "use $class";
    die "Unsupported TypeKey driver $class: $@" if $@;
    my $driver = bless {}, $class;
    $driver->{key}        = $app;
    $driver->{sig_window} = 600;
    $driver->init(@_) or return $class->error($driver->errstr);
    $driver;
}

sub init { $_[0] }

sub cookie_name     { 'tk_commenter' }
sub typekey_script  { MT->config->CGIPath . 'plugins/typekey/app.cgi' }    # FIX
sub typekey_version { MT->config->TypeKeyVersion }
sub signon_url      { MT->config->SignOnURL }
sub signoff_url     { MT->config->SignOffURL }
sub regkey_url      { MT->config->RegKeyURL }
sub need_email      { 1 }
sub token { MT->instance->blog->remote_auth_token if MT->instance->blog; }

sub signon_params;
sub signoff_params;
sub post_signon_link;
sub post_signoff_link;

sub state_mgr {
    my $self = shift;
    return $self->{state_mgr} if $self->{state_mgr};
    require MT::StateMgr;
    my $mgr =
      MT::StateMgr->new(
                        {
                         script      => $self->typekey_script,
                         cookie_name => $self->cookie_name,
                         token       => $self->token,
                         need_email  => $self->need_email
                        }
      )
      or return $self->error(MT::StateMgr->errstr);
    $self->{state_mgr} = $mgr;
    $mgr;
}

sub validate {
    my ($self, $q) = @_;    # $q must be param object. hash support?
    my $tk = Authen::TypeKey->new;
    $tk->token($self->token);
    $tk->expires($self->{sig_window});    # sig_window should be param?
    $tk->version($self->typekey_version);

#    $tk->key_cache(File::Spec->tmpdir); # this doesn't work for reasons unknown
    $tk->key_url($self->regkey_url);
    $tk->verify($q) or $self->error($tk->errstr);
}

#--- link generators

sub signon_link {
    my ($self, $ctx) = @_;
    my $p = $self->signon_params($ctx) || {};
    $p->{app} = $self->{key};
    require URI;
    my $return = URI->new($self->typekey_script);
    $return->query_form(%$p);
    my $uri = URI->new($self->signon_url);
    $uri->query_form(
                     t          => $self->token,
                     v          => $self->typekey_version,
                     need_email => $self->need_email,
                     '_return'  => $return->as_string
    );
    $uri->as_string;
}

sub signoff_link {
    my ($self, $ctx) = @_;
    my $p = $self->signoff_params($ctx) || {};
    $p->{logout} = 1;
    $p->{app}    = $self->{key};
    require URI;
    my $return = URI->new($self->typekey_script);
    $return->query_form(%$p);
    my $uri = URI->new($self->signoff_url);
    $uri->query_form('_return' => $return->as_string);
    $uri->as_string;
}

1;

__END__

=head1 NAME

MT::TypeKey - a base class for creating Keys Framework
drivers for TypeKey-enabled applications.

=head1 VERSION

0.4

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head1 AUTHOR & COPYRIGHT

MT::Typekey is Copyright 2005-2007, Timothy Appnel, Appnel
Internet Solutions, LLC. http://appnel.com/

This program is free software; you can use, modify, and
redistribute it under the same terms as Perl 5.8.x itself.

See http://www.perl.com/perl/misc/Artistic.html

=cut
